package com.dzl.express.utils;

import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;

@Slf4j
public class KuaiIdi100Utils {
	//公司编号
	private static final String CUSTOMER="5DCEC87BABBFB4C8F081770BB80117C8";
	//快递100key
	private static final String KUAIDI100_KEY="fPOReaZC2487";

	/**
	 * 实时查询快递信息 间隔最少控制在30分钟以上  否则会造成锁单
	 * @date 2021/5/21 11:14
	 * @author lishunxing
	 * @param code 快递编号
	 * @param expressNo 快递单号
	 * @return com.dzl.util.Kuaidi100Response
	 */
	public static Result getExpressInfo(String type,String num){
		String param ="{\"com\":\""+type+"\",\"num\":\""+num+"\"}";

		String sign = MD5.encode(param+KUAIDI100_KEY+CUSTOMER);
		HashMap<String, String> params = new HashMap<String, String>();
		params.put("param",param);
		params.put("sign",sign);
		params.put("customer",CUSTOMER);
		
		String resultJson = null;
		try {
			resultJson = HttpRequest.postData("http://poll.kuaidi100.com/poll/query.do", params, "utf-8").toString();
			Gson gson=new Gson();
			Result result = gson.fromJson(resultJson, Result.class);
			 
			if(null==result || !"ok".equals(result.getMessage())){
				return null;
			}
			
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			log.error("["+type+"-"+num+"]获取快递信息失败");
			return null ;
		}
	}
	/*public static Result getExpressInfo(String type,String num){

		HashMap<String, String> params = new HashMap<String, String>();
		params.put("type",type);
		params.put("postid",num);
		params.put("temp",String.valueOf(Math.random()));
		params.put("phone","");
		
		String resultJson = null;
		try {
			resultJson = HttpRequest.postData("https://www.kuaidi100.com/query", params, "utf-8").toString();
			Gson gson=new Gson();
			Result result = gson.fromJson(resultJson, Result.class);
			 
			if(null==result || !"ok".equals(result.getMessage())){
				return null;
			}
			
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("["+type+"-"+num+"]获取快递信息失败");
			return null ;
		}
	}*/
}
